﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text;

namespace T.FAS.Runtime.Base.Entity
{
    [Serializable]
    public class TArray<T>:List<T>
    {
        private TEntity entity;
        public TArray(TEntity entity)
        {
            this.entity = entity;
            TName = entity.MetadataCode;
        }
        public string TName { get; set; }
        public int CurrentRowNumber { get; set; }
        public int Rows { get; set; }

        //[JsonIgnore]
        public TRecord Current
        {
            get
            {
                if (entity.MetadataType == TEntityType.DQ)//平板结构转主子树形结构
                {
                    Dictionary<string, object> dResult = new Dictionary<string, object>();
                    TRecord curItem = this[CurrentRowNumber] as TRecord;
                    foreach (var item in entity.Attributes)
                    {
                        if (item.DataType == DataType.Record)
                        {
                            Dictionary<string, object> cResult = new Dictionary<string, object>();
                            foreach (var cItem in item.Entity.Attributes)
                            {
                                cResult.Add(cItem.SourceColumnCode, curItem[cItem.Code]);
                            }
                            TRecord nItem = new TRecord(cResult);
                            dResult.Add(item.Code, nItem);
                        }
                        else
                        {
                            dResult.Add(item.Code, curItem[item.Code]);
                        }
                    }
                    return new TRecord(dResult);
                }
                else
                {
                    return new TRecord(
                        new Dictionary<string, object>()
                        {
                        { TName,this[CurrentRowNumber]}
                        });
                }
            }
        }
    }
}

